/*
 * Copyright 2018 the original author or authors.
 *
 * you can use the file or import project.but Do not take legal responsibility.
 * Only for learning communication.
 *
 * Java compiled version 1.8 or higher
 *
 *
 *
 *
 */

package org.liang.javasource.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

/**
 * TODO 描述信息
 * 
 * @author houliang
 * @date 2018年3月5日
 */
public abstract class FileReaderUtil {

	public static String read(InputStream inputStream, String charSet)
			throws UnsupportedEncodingException, IOException {
		StringBuffer sb = new StringBuffer();
		int n = -1;
		byte[] b = new byte[512];
		while ((n = inputStream.read(b)) != -1) {
			String temp = new String(b, 0, n, charSet);
			sb.append(temp);
		}

		inputStream.close();
		return sb.toString();
	}

	public static String read(InputStream inputStream)
			throws UnsupportedEncodingException, IOException {
		return read(inputStream, "utf-8");
	}

	public static String readOriginal(InputStream inputStream)
			throws UnsupportedEncodingException, IOException {
		StringBuffer sb = new StringBuffer();
		Scanner sc = new Scanner(inputStream);

		while ((sc.hasNextLine())) {
			sb.append(sc.nextLine()).append("\r\n");
		}
		sc.close();
		return sb.toString();
	}

}
